<template>
  <div class='home-container'>
    <el-container class='home-container'>
      <!-- 头部区 -->
      <el-header>
        <!-- 左侧图标区 -->
        <div class='left-img'>
          <img src='../assets/wofe.jpg' alt='' class='img1' />
          <span style='margin-left: 15px'>电商管理系统</span>
        </div>
        <el-button type='info' @click='loginOut'>退出</el-button>
      </el-header>
      <!-- 页面主题区域 -->
      <el-container>
        <!-- 侧边栏 -->
        <!-- 根据折叠变宽度66 -->
        <el-aside
          :width='isCollapse ? "64px" : "200px"'
          :transform='isCollapse ? "all 1s" : "all 0s"'
        >
          <div class='toggle-button' @click='toggleCollapse'>|||</div>
          <!-- 左侧菜单 -->
          <el-menu
            background-color='#333744'
            text-color='#fff'
            active-text-color='#ffd04b'
            unique-opened
            :collapse='isCollapse'
            :collapse-transition='false'
            :router='true'
            :default-active='activePath'
          >
            <!-- 数字型加字符等于字符还可以用toString()转换 -->
            <el-submenu
              :index='item.id + ""'
              v-for='item in menulist'
              :key='item.id'
            >
              <!-- 一级菜单的模板 -->
              <template slot='title'>
                <i :class='iconObj[item.id]'></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 一级菜单下的2级菜单 -->
              <!-- 不用id跳转了用path -->
              <!-- 绑定一个高亮事件 -->
              <el-menu-item
                :index='"/" + subItem.path'
                v-for='subItem in item.children'
                :key='subItem.id'
                @click='saveNveStatus("/" + subItem.path)'
              >
                <template slot='title'>
                  <i class='el-icon-menu'></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 大区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      iconObj: {
        125: 'iconfont icon-yonghuquanxian',
        103: 'iconfont icon-quanxianguanli',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-dingdanguanli',
        145: 'iconfont icon-shujutongji'
      },
      // 默认不折叠
      isCollapse: false,
      // 被激活的链接
      activePath: ''
    }
  },
  // 生命周期
  created () {
    // 创建一个获取左侧菜单数据的方法
    this.getMenuList()
    // 获取高亮的值用get
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut () {
      // 将获取的token清空
      window.sessionStorage.clear()
      // 通过编程式导航跳转到登录页面
      this.$router.push('/login')
    },
    // 获取菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res);
      // console.log(this.menulist);
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNveStatus (activePath) {
      // 将值变化用set
      window.sessionStorage.setItem('activePath', activePath)
      // 获取生命周期函数一开始的值上传到data再到页面
      this.activePat = activePath
    }
  }
}
</script>

<style Lang='less' scoped>
.img1 {
  width: 80px;
  height: 60px;
  border-radius: 50%;
}

.home-container {
  height: 100%;
}

.el-header {
  background-color: #666666;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #ffffff;
  line-height: 60px;
  font-size: 20px;
}

/* 让内部嵌套居中 */
.left-img {
  display: flex;
  align-items: center;
}

.el-aside {
  background-color: #333744;
  color: #ffffff;
  /* text-align: center; */
}

/* 我套不进去 */
.el-menu {
  border-right: none;
}

/* 后面要改 */
.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  text-align: center;
  background-color: #999999;
  font-size: 10px;
  line-height: 24px;
  font-weight: 600;
  /* 字符串间距 */
  letter-spacing: 0.3em;
  /* 变成小手鼠标 */
  cursor: pointer;
}
</style>
